﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Marbles.Core.Connectors;

namespace Marbles.Core.BBS
{
    public class ForumPostCollection : ICollection<ForumPost>
    {

        public Forum Forum { get { return _forum; } }
        public ForumPost Parent { get { return _parent; } }
        public ForumPost this[int index] { get { return items[index]; } set { items[index] = value; } }
        public int Count
        {
            get { return items.Count; }
        }

        public bool ApprovedPostsOnly { get { return _approvedonly; } }

        public bool IsReadOnly
        {
            get { return false; }
        }

        private ForumPost _parent = null;
        private Forum _forum = null;
        private List<ForumPost> items = null;
        private bool _approvedonly = false;

        internal ForumPostCollection()
        {
            items = new List<ForumPost>();
        }

        public ForumPostCollection(string siteid, string forumname, bool approvedonly)
            : this()
        {
            _approvedonly = approvedonly;
            _forum = new Forum(siteid, forumname);
            _parent = null;
            Load();
        }

        public ForumPostCollection(Forum Forum, bool approvedonly)
            : this()
        {
            _approvedonly = approvedonly;
            _parent = null;
            _forum = Forum;
            Load();
        }

        public ForumPostCollection(Forum Forum, ForumPost Parent, bool approvedonly)
            : this()
        {
            _approvedonly = approvedonly;
            _parent = Parent;
            _forum = Forum;
            Load();
        }

        public ForumPostCollection(string siteid, string forumname, Int64 postid, bool approvedonly)
            : this()
        {
            _approvedonly = approvedonly;
            _forum = new Forum(siteid, forumname);
            _parent = new ForumPost(siteid, forumname, postid);
            Load();
        }

        private void Load()
        {
            DataConnection dbc = new DataConnection();
            Command cmd = null;

            if (_approvedonly)
            {
                cmd = new Command("SELECT * FROM forumitems WHERE siteid = @siteid AND forumname = @forumname AND parentpostid = @postid AND approved = 1 ORDER BY sticky DESC, postdate DESC");
            }
            else
            {
                cmd = new Command("SELECT * FROM forumitems WHERE siteid = @siteid AND forumname = @forumname AND parentpostid = @postid ORDER BY sticky DESC, postdate DESC");
            }

            cmd.AddParameter("@siteid", _forum.Siteid);
            cmd.AddParameter("@forumname", _forum.ForumName);
            if (_parent == null)
            {
                cmd.AddParameter("@postid", 0);
            }
            else
            {
                cmd.AddParameter("@postid", _parent.PostId);
            }

            DataTable dt = dbc.FetchDataTable(cmd);
            Clear();

            foreach (DataRow dr in dt.Rows)
            {
                items.Add(new ForumPost(dr));
            }
        }

        public void Add(ForumPost item)
        {
            items.Add(item);
        }

        public void Clear()
        {
            items.Clear();
        }

        public bool Contains(ForumPost item)
        {
            return items.Contains(item);
        }

        public void CopyTo(ForumPost[] array, int arrayIndex)
        {
            items.CopyTo(array, arrayIndex);
        }

        public bool Remove(ForumPost item)
        {
            return items.Remove(item);
        }

        public IEnumerator<ForumPost> GetEnumerator()
        {
            return items.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return items.GetEnumerator();
        }
    }
}
